﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Common.Utils
{
    public class TimeGetDevCap
    {
        [DllImport("winmm.dll")] internal static extern uint timeGetDevCaps(ref timecaps_tag times, uint period);

        [DllImport("winmm.dll")] public static extern uint timeBeginPeriod(uint period);

        [DllImport("winmm.dll")] internal static extern uint timeEndPeriod(uint period);

        #region 参考文档
        /*
        https://docs.microsoft.com/en-us/windows/win32/api/timeapi/nf-timeapi-timegetdevcaps
        */
        #endregion

        [DllImport("Kernel32.dll")]
        public static extern uint  SetThreadAffinityMask(IntPtr hThread, uint dwThreadAffinityMask);
  //[in] HANDLE hThread,
  //[in] DWORD_PTR dwThreadAffinityMask
  //);

        public struct timecaps_tag
        {
           public uint wPeriodMin;
           public uint wPeriodMax;
        }

        public static bool SetMinTimecaps()
        {
            timecaps_tag tag = new timecaps_tag();
            uint size = 8;
            var result = timeGetDevCaps(ref tag, size);
            if (result != 0)
            {
                return false;
            }

            result = timeBeginPeriod(tag.wPeriodMin);

            if (result != 0)
            {
                return false;
            }

            return true;
        }
    }
}
